package com.tanhua.server.controller;


import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.service.MessageService;
import org.apache.catalina.startup.RealmRuleSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.Result;
import java.util.Map;

/**
 * @program: tanhua
 * @description:
 * @author: MR.peng
 * @create: 2022-03-27 14:47
 **/
@RestController
@RequestMapping("/messages")
public class MessageController {

    @Autowired
    private MessageService messageService;

    /*
    * 添加联系人：
    *   POST /contacts
    * 请求参数：
    *  Body userId
    *   返回值：null
    * */
    @PostMapping("/contacts")
    public ResponseEntity contacts(@RequestBody Map map){
        String temp = map.get("userId").toString();
        Long friendId = Long.valueOf(temp);
        messageService.contacts(friendId);
        return ResponseEntity.ok(null);
    }

    /*
    * 联系人列表：
    *   路径： GET /contacts
    *   参数： Query pagesize,page
    *   返回值：PageResult  List<Vo>
    * */


    @GetMapping("/contacts")
    public ResponseEntity contacts(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer pagesize){
       PageResult vo =  messageService.findContacts(page,pagesize);
       return  ResponseEntity.ok(vo);
    }
    /**
     * 为了解决聊天页面头像和昵称不匹配问题
     * GET /messages/userinfo
     * 参数 huanxinId 环信用户id
     * 返回 userInfoVo
     */
    @GetMapping("/messages/userinfo")
    public ResponseEntity userInfo(String huanxinId){
        UserInfoVo userInfoVo = messageService.findById(huanxinId);
        return ResponseEntity.ok(userInfoVo);
    }
}
